<!--
  @Description:新增/编辑页面基础信息
  @Author: zqy
  @Date: 2022-02-11 16:15:16
  @Last Modified by:   zqy
  @Last Modified time: 2020-07-20 16:15:16
 -->
<template>
  <el-dialog
    ref="formDlg"
    title="页面基础信息配置"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="500px"
    @close="closeDialog"
  >
    <div style="padding-right:30px">
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="页面名称:" prop="pageName">
              <el-input v-model.trim="formData.pageName" maxlength="25" show-word-limit placeholder="请输入页面名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="页面图标:">
              <div class="img-upload-container">
                <qup-img-uploader
                  :width="100"
                  :height="100"
                  mask
                  :image-url="cateImage.absoluteUrl"
                  :relative-url="cateImage.relativeUrl"
                  @upload-success="handleUploadSuccess"
                  @remove="handleDelImg"
                />
                <div class="icon-msg">建议图片尺寸大小100*100,不超过1M,支持png\jpg\jpeg</div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
      <el-button size="mini" type="primary" @click="submitForm('config')">保存并去配置楼层</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { savePageBasic } from '../api/helpCenter'
import QupImgUploader from '@/components/imgUploader'
export default {
  name: 'HelpContentBasicEdit',
  components: { QupImgUploader },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    basicData: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        id: null,
        pageName: '',
        iconUrl: ''
      },
      formRules: {
        pageName: [
          { required: true, message: '请输入页面名称', trigger: 'blur' }
        ]
      },
      cateImage: {
        absoluteUrl: '',
        relativeUrl: ''
      },
      submiting: false
    }
  },

  computed: {
    dlgTitle() {
      return this.formData && this.formData.id ? '编辑页面基础信息配置' : '新增页面基础信息配置'
    },
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    if (this.basicData && this.basicData.id) {
      const data = this.basicData
      this.formData = {
        id: data.id,
        pageName: data.pageName,
        iconUrl: data.iconUrl
      }
      this.cateImage = {
        absoluteUrl: this.formatImageUrl(data.iconUrl),
        relativeUrl: data.iconUrl
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        id: null,
        pageName: '',
        iconUrl: ''
      }
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    submitForm(type) {
      if (this.submiting) return
      this.$refs['refForm'].validate(async(valid) => {
        if (valid) {
          const formData = Object.assign(this.formData)
          this.submiting = true
          await savePageBasic(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                // message: res.msg ? res.msg : `${formData.id ? '编辑' : '新增'}保存成功`,
                message: `${formData.id ? '编辑' : '新增'}保存成功`,
                type: 'success'
              })
              this.closeDialog()
              const data = {
                ...{
                  pageName: formData.pageName,
                  iconUrl: formData.iconUrl
                },
                ...res.data
              }
              const result = {
                data,
                goConfig: type === 'config'
              }
              this.$emit('success', result)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    },
    handleUploadSuccess(res, file, fileList) {
      this.formData.iconUrl = res.storeFileName
      this.cateImage = {
        absoluteUrl: res.url,
        relativeUrl: res.storeFileName
      }
    },
    handleDelImg() {
      this.formData.iconUrl = ''
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
  .img-upload-container {
    .icon-msg{
      margin-top: 10px;
      color: #999;
    }
  }
</style>
